#include "Painter.h"

class Maurio
{

public:

	Maurio()
	{
		m_Sprite = Root::GetSingleton().GetRenderer()->CreateSprite("Ship");
		m_View = Root::GetSingleton().GetRenderer()->GetViewport(1);
		m_WorldPos.x = 30.f;
		m_WorldPos.y = 60.f;
	}
	~Maurio() { }

	void Tick()
	{
		float moved = 0.f;

		if (Keyboard::Get().Pressed(YGK_RIGHT))
		{
			m_WorldPos.x += 3.f;			
		}
		if (Keyboard::Get().Pressed(YGK_LEFT))
		{
			m_WorldPos.x -= 3.f;
		}
		if (Keyboard::Get().Pressed(YGK_UP))
		{
			m_WorldPos.y -= 3.f;
		}
		if (Keyboard::Get().Pressed(YGK_DOWN))
		{
			m_WorldPos.y += 3.f;
		}

		m_ScreenPos = m_View->GetCenterPosition(m_WorldPos);
	}

	void Render()
	{
		m_Sprite->Draw(m_ScreenPos.x, m_ScreenPos.y);
	}

private:

	Sprite* m_Sprite;
	Viewport* m_View;
	Vec2 m_WorldPos;
	Vec2 m_ScreenPos;
	
}; // class Maurio

Surface* surf;
Background* g_Test;

Game* Game::Configure()
{
	Root::GetSingleton().SetProperty(Root::PROP_WINDOWTITLE, "Painter");

	Root::GetSingleton().InstallRenderer("RenderMethodDirectDraw");
	Root::GetSingleton().InstallRenderer("RenderMethodOpenGLES");

	return new Painter;
}

Painter::Painter()
{

}

Painter::~Painter()
{

}

void Painter::Init()
{
	uint32 id = Root::GetSingleton().GetRenderer()->CreateViewport(240, 320, YG_VIEWPORT_SCALEX);
	//uint32 id = Root::GetSingleton().GetRenderer()->CreateViewport(320, 320, YG_VIEWPORT_NOTHING);
	Root::GetSingleton().GetRenderer()->SetViewport(id);
	m_View = Root::GetSingleton().GetRenderer()->GetViewport(id);
	m_View->SetBounds(Vec2(16, 16));
	m_View->SetWorldDimensions(480, 480);

	m_Surf = Root::GetSingleton().GetRenderer()->CreateSurface(240, 320);
	m_Surf->Fill(Color(1.f, 1.f, 1.f));

	bool result = false;
	//result = Root::GetSingleton().GetRenderer()->LoadImage("media\\wallpaper.png", "Wallpaper");
	result = Root::GetSingleton().GetRenderer()->LoadImage("media/Viper_Mark_II.bmp", "Ship",  Color::CBLACK);
	result = Root::GetSingleton().GetRenderer()->LoadImage("media/alien.bmp", "Alien",         Color::CFUCHSIA);

	g_Test = Root::GetSingleton().GetRenderer()->CreateBackground("Alien");

	m_Maurio = new Maurio();

	for (uint32 i = 0; i < 32; ++i)
	{
		m_Objects[i] = Root::GetSingleton().GetRenderer()->CreateSprite("Alien");
		m_ObjectsPos[i].x = Math::Random(0, 480.f);
		m_ObjectsPos[i].y = Math::Random(0, 480.f);
	}
}

void Painter::Update()
{
	if (Keyboard::Get().Pressed(YGK_OK)) { Root::GetSingleton().CloseDown(); }

	m_Maurio->Tick();
}

void Painter::Render()
{
	//g_Test->Draw(0, 0, true, true);
	
	//m_Surf->Draw(0, 0);

	glClearColor(1.f, 0.f, 0.f, 1.f);
	glClear(GL_COLOR_BUFFER_BIT);

	for (uint32 i = 0; i < 32; ++i)
	{
		Vec2 draw_pos = m_View->GetScreenPosition(m_ObjectsPos[i]);
		m_Objects[i]->Draw(draw_pos.x, draw_pos.y);
	}

	m_Maurio->Render();

	glClearColor(0.f, 0.f, 0.f, 1.f);

	//surf->Draw(0, 0);
}

void Painter::Resize(int a_Width, int a_Height)
{
	
}